package yyee.llee.mmee.viewmode

import android.os.Build
import androidx.annotation.RequiresApi
import androidx.lifecycle.MutableLiveData
import com.google.gson.Gson
import com.google.gson.reflect.TypeToken
import yyee.llee.mmee.base_api_net.base_api_bean.WeatherHour
import yyee.llee.mmee.base_api_net.base_api_bean.bean.BaseResult
import yyee.llee.mmee.base_api_net.base_api_bean.bean.CalendarBean
import yyee.llee.mmee.base_api_net.base_api_bean.bean.GoodArticle
import yyee.llee.mmee.base_api_net.base_api_bean.bean.GuideToLifeBean
import yyee.llee.mmee.base_api_net.base_api_bean.bean.ToadyWeatherBean
import yyee.llee.mmee.pl190.host668.StrongContext
import yyee.llee.mmee.weather.repository.WeatherRepository
import com.ultra.kingclean.cleanmore.bean.*
import kotlinx.coroutines.Dispatchers
import kotlinx.coroutines.withContext
import java.text.SimpleDateFormat
import java.util.*

class WeatherChildViewModel : BaseViewModel() {

    val weatherNow = MutableLiveData<ToadyWeatherBean>()
    val weatherHour = MutableLiveData<WeatherHour>()
    val suggestionBean = MutableLiveData<GuideToLifeBean>()
    val calendarBean = MutableLiveData<CalendarBean>()
    val goodArticle = MutableLiveData<List<GoodArticle>>()
    private val weatherRepository = WeatherRepository()


    fun loadData(cityCode: String) {

        launchUI {
            val result = withContext(Dispatchers.IO) {
                weatherRepository.getWeatherNow(cityCode)
            }
            if (result is BaseResult.Success) {
                weatherNow.postValue(result.data)
            } else if (result is BaseResult.Error) {
                mExceptionLiveData.postValue(result.exception)
            }
        }


        launchUI {
            val result = withContext(Dispatchers.IO) {
                weatherRepository.getSuggestion(cityCode)
            }
            if (result is BaseResult.Success) {
                suggestionBean.postValue(result.data)
            } else if (result is BaseResult.Error) {
                mExceptionLiveData.postValue(result.exception)
            }
        }

        launch {
            val list: List<GoodArticle> = Gson().fromJson(
                bi.GD.getJson(
                    "good_article.json",
                    StrongContext.getContext()
                ),
                object : TypeToken<List<GoodArticle>>() {}.type
            )
            goodArticle.postValue(list)
        }
    }


    @RequiresApi(Build.VERSION_CODES.N)
    fun getCalendar() {
        launchUI {
            val result = withContext(Dispatchers.IO) {
                val time = getDayFromTime()
                weatherRepository.getCalendar(time!!)
            }
            if (result is BaseResult.Success) {
                calendarBean.postValue(result.data)
            } else if (result is BaseResult.Error) {
                mExceptionLiveData.postValue(result.exception)
            }
        }
    }


    fun getDayFromTime(): String? {
        val currentTime = System.currentTimeMillis()
        val date = Date(currentTime)
        val day = SimpleDateFormat("YYYY-MM-DD")
        return day.format(date)
    }
}